package com.yf1706.service;

import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.yf1706.bean.CountAbsenceDeduct;
import com.yf1706.bean.FloatSalarySubsidy;
import com.yf1706.dao.jpa.ICountAbsenceDeductDAO;
import com.yf1706.dao.jpa.IFloatSalarySubsidyJDAO;

/**
 * 缺勤扣款和全勤奖
 * 
 * @author 锦涛
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class CountAbsenceDeductService {

	@Autowired
	private ICountAbsenceDeductDAO cAbsenceDeductDAO;
	
	@Autowired
	private IFloatSalarySubsidyJDAO SSubsidyDAO;

	//------------------缺勤扣款-------------------
	/**
	 * 增加缺勤扣款
	 */
	@Transactional
	public void save(CountAbsenceDeduct countAbsenceDeduct) {
		cAbsenceDeductDAO.save(countAbsenceDeduct);
	}

	/**
	 * 根据ID删除缺勤扣款
	 * @param ids
	 */
	@Transactional
	public void remove(String ids) {
		for (String id : ids.split(",")) {
			if (id != null) {
				cAbsenceDeductDAO.deleteById(Integer.parseInt(id));
			}
		}
	}

	/**
	 * 修改数据
	 * @param cRate 先根据要修改的对象id查询出对应的持久化对象 直接调用持久化对象的set方法修改对象的数据
	 */
	@Transactional
	public void update(CountAbsenceDeduct countAbsenceDeduct) {
		Optional<CountAbsenceDeduct> sessionCOverSalary = cAbsenceDeductDAO.findById(countAbsenceDeduct.getAbsenceDeductId());
		sessionCOverSalary.get().setAbsenceDeductType(countAbsenceDeduct.getAbsenceDeductType());
		sessionCOverSalary.get().setAbsenceDeductValue(countAbsenceDeduct.getAbsenceDeductValue());
	}

	/**
	 * 分页查询缺勤扣款
	 */
	@Transactional
	public Page<CountAbsenceDeduct> querybypage(int page, int rows, String sort, String order) {
		if (order.equals("desc")) {
			Pageable pageable = PageRequest.of(page - 1, rows, Sort.Direction.DESC, sort);
			return cAbsenceDeductDAO.findAll(pageable);
		} else {
			Pageable pageable = PageRequest.of(page - 1, rows, Sort.Direction.ASC, sort);
			return cAbsenceDeductDAO.findAll(pageable);
		}
	}
	//--------------全勤奖---------------
	@Transactional
	public void updateSubsidy(double subsidyValue) {
		FloatSalarySubsidy SessionSubsidy = SSubsidyDAO.getOne(1);
		SessionSubsidy.setSubsidyValue(subsidyValue);
	}
	
	@Transactional
	public double querySubsidyValue() {
		return SSubsidyDAO.getOne(1).getSubsidyValue();
	}
}
